import Dialog from "../../vant/dist/dialog/dialog"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    sumE: 0.00,
    sumI: 0.00,
    sumB: 0.00,
    All: [],
    detail: {},
    show: false,
    empty: true,
    year: new Date().getFullYear(),
    month: new Date().getMonth() + 1,
    currentDate: new Date().getTime(),
    minDate: new Date().getTime() - 315360000000,
  },
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
  showPopup() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  checkedTime() {
    var time = this.data.currentDate;
    var user = this.data.user;
    console.log(new Date(time).getFullYear());
    var month = new Date(time).getMonth() + 1;
    var year = new Date(time).getFullYear();
    if (month < 10) {
      month = "0" + month
    }
    var {
      sumE,
      sumI,
      sumB
    } = this.handleCount(user.detail);
    var {
      All,
      obj
    } = this.handleArr(user.detail);
    var objs = {};
    for (var key in obj) {
      var keys = key.substr(5, 6)
      objs[keys] = obj[key]
    }
    this.setData({
      month,
      year,
      sumE,
      sumI,
      sumB,
      All,
      detail: objs
    })
    this.onClose()
  },
  handleCount(arr) {
    var TotalE = [];
    var TotalI = [];
    var sumE = 0;
    var sumI = 0;
    arr.forEach(item => {
      item.Statistics.forEach(child => {
        if (child.money < 0 && new Date(this.data.currentDate).getFullYear() + "." + new Date(this.data.currentDate).getMonth() + 1 == new Date(child.date).getFullYear() + "." + new Date(child.date).getMonth() + 1) {
          TotalE.push(child.money)
        }
        if (child.money > 0 && new Date(this.data.currentDate).getFullYear() + "." + new Date(this.data.currentDate).getMonth() + 1 == new Date(child.date).getFullYear() + "." + new Date(child.date).getMonth() + 1) {
          TotalI.push(child.money)
        }
      })
    })
    // console.log(TotalE);
    // console.log(TotalI);
    if (TotalE.length != 0) {
      sumE = TotalE.reduce((a, b) => {
        return a + b;
      })
    }
    if (TotalI.length != 0) {
      sumI = TotalI.reduce((a, b) => {
        return a + b;
      })
    }
    var obj = {
      sumE: sumE.toFixed(2),
      sumI: sumI.toFixed(2),
      sumB: (sumI + sumE).toFixed(2)
    }
    return obj
  },
  handleArr(arr) {
    var All = [];
    arr.forEach(item => {
      item.Statistics.forEach(child => {
        if (new Date(this.data.currentDate).getFullYear() + "." + new Date(this.data.currentDate).getMonth() + 1 == new Date(child.date).getFullYear() + "." + new Date(child.date).getMonth() + 1) {
          All.push(child)
        }
      })
    })
    All.sort(function (a, b) {
      return b.date - a.date;
    })
    var obj = {};
    var arr = [];
    var empty;
    All.forEach(item => {
      // console.log(item);
      var time = new Date(item.date);
      var day = time.getFullYear() + "年" + (time.getMonth() + 1) + "月" + time.getDate() + "日";
      var month = time.getFullYear() + "年" + (time.getMonth() + 1) + "月";
      var currentDate = new Date(this.data.currentDate).getFullYear() + "年" + (new Date(this.data.currentDate).getMonth() + 1) + "月";
      if (!(Object.keys(obj).includes(day)) && month == currentDate) {
        obj[day] = [];
        obj[day].push(item)
      } else {
        obj[day].push(item)
      }
    })
    console.log(obj);
    if (Object.keys(obj).length != 0) {
      empty = false
    } else {
      empty = true
    }
    this.setData({
      empty
    })
    return {
      All,
      obj
    }
  },
  goRecord() {
    wx.redirectTo({
      url: './extenditure/extenditure',
    })
  },
  onDelete(event) {
    const {
      position,
      instance
    } = event.detail;
    let {
      id
    } = event.currentTarget.dataset;
    let All = this.data.All;
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除吗？',
        }).then(() => {
          instance.close();
          All.forEach(item => {
            if (item.id == id) {
              let index = All.indexOf(item);
              All.splice(index, 1)
            }
          })
          this.setData({
            All
          })
        }).catch(() => {
          instance.close();
          console.log(2);
        });
        break;
    }
  },
  handleData(res) {
    var {
      sumE,
      sumI,
      sumB
    } = this.handleCount(res.data[0].detail);
    var {
      All,
      obj
    } = this.handleArr(res.data[0].detail)
    var objs = {};
    for (var key in obj) {
      var keys = key.substr(5, 6)
      objs[keys] = obj[key]
    }
    this.setData({
      user: res.data[0],
      sumE,
      sumI,
      sumB,
      All,
      detail: objs
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.request({
      url: `http://127.0.0.1:7001/user`,
      success: res => {
        // console.log(res);
        this.handleData(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})